using Microsoft.AspNetCore.Mvc;
using WebRtcProject.Models;
using WebRtcProject.Models.Messages;
using WebRtcProject.Services;

namespace WebRtcProject.Controllers
{
    /// <summary>
    /// WebRTC 嵌入式设备管理控制器
    /// </summary>
    [ApiController]
    [Route("api/webrtc-devices")]
    public class WebRtcDeviceController : ControllerBase
    {
        private readonly IWebRtcRabbitMQService _webRtcService;
        private readonly ILogger<WebRtcDeviceController> _logger;

        public WebRtcDeviceController(
            IWebRtcRabbitMQService webRtcService,
            ILogger<WebRtcDeviceController> logger)
        {
            _webRtcService = webRtcService;
            _logger = logger;
        }

        /// <summary>
        /// 获取服务状态
        /// </summary>
        [HttpGet("service/status")]
        public async Task<ActionResult> GetServiceStatus()
        {
            try
            {
                var status = await _webRtcService.GetServiceStatusAsync();
                return Ok(status);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取服务状态失败");
                return StatusCode(500, new { error = "获取服务状态失败", message = ex.Message });
            }
        }

        /// <summary>
        /// 注册设备
        /// </summary>
        [HttpPost("{deviceId}/register")]
        public async Task<ActionResult> RegisterDevice(string deviceId, [FromBody] RegisterDeviceRequest request)
        {
            try
            {
                var capabilities = new DeviceCapabilities
                {
                    DeviceType = request.DeviceType,
                    AudioCodecs = request.AudioCodecs ?? new List<string>(),
                    MaxConcurrentCalls = request.MaxConcurrentCalls,
                    BandwidthLimit = request.BandwidthLimit,
                    SupportsDTMF = request.SupportsDTMF,
                    FirmwareVersion = request.FirmwareVersion
                };

                var success = await _webRtcService.RegisterDeviceAsync(deviceId, capabilities);
                
                if (success)
                {
                    return Ok(new { message = "设备注册成功", deviceId, timestamp = DateTime.UtcNow });
                }
                else
                {
                    return StatusCode(500, new { error = "设备注册失败" });
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "注册设备失败: {DeviceId}", deviceId);
                return StatusCode(500, new { error = "注册设备失败", message = ex.Message });
            }
        }

        /// <summary>
        /// 获取在线设备列表
        /// </summary>
        [HttpGet("online")]
        public async Task<ActionResult> GetOnlineDevices()
        {
            try
            {
                var devices = await _webRtcService.GetOnlineDevicesAsync();
                return Ok(new { devices, count = devices.Count, timestamp = DateTime.UtcNow });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取在线设备失败");
                return StatusCode(500, new { error = "获取设备列表失败", message = ex.Message });
            }
        }
    }

    public class RegisterDeviceRequest
    {
        public string DeviceType { get; set; } = "voice_phone";
        public List<string>? AudioCodecs { get; set; }
        public int MaxConcurrentCalls { get; set; } = 1;
        public int BandwidthLimit { get; set; } = 64;
        public bool SupportsDTMF { get; set; } = true;
        public string? FirmwareVersion { get; set; }
    }
} 